from django.db import models
from user.models import User
import os


# Create your models here.
class Category(models.Model):
    class Meta:
        verbose_name = verbose_name_plural = '课程种类表'

    name = models.CharField(max_length=128, unique=True, verbose_name='课程种类')

    def __str__(self):
        return self.name


def save_video_file(instance, filename):
    return os.path.join('video', filename)


def save_img_file(instance, filename):
    return os.path.join('static', 'img', filename)


class Course(models.Model):
    STATUS_CHOICES = (
        (0, '收费'),
        (1, '免费')
    )

    class Meta:
        verbose_name = verbose_name_plural = '课程表'

    courseName = models.CharField(max_length=128, verbose_name='课程名称')
    fileName = models.FileField(upload_to=save_video_file, verbose_name='文件名称')
    imgName = models.ImageField(upload_to=save_img_file, verbose_name='课程图片')
    category = models.ForeignKey(to=Category, on_delete=models.CASCADE, related_name='categary_set',
                                 verbose_name='课程类别')
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name='价格', blank=True)
    summary = models.CharField(max_length=1024, default='', verbose_name='课程介绍', blank=True)
    status = models.PositiveSmallIntegerField(default=0, verbose_name='状态', choices=STATUS_CHOICES)  # 0:收费 1:免费
    createDatetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    userBuyer = models.ManyToManyField(to=User, related_name='userBuyer_set', verbose_name='购买用户', blank=True)
    userShoppingcart = models.ManyToManyField(to=User, related_name='userShoppingcart_set', verbose_name='加入购物车的用户',
                                              blank=True)

    def __str__(self):
        return self.courseName
